package com.lanling.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanling.eduorder.entity.Order;
import com.lanling.eduorder.entity.OrderQuery;
import com.lanling.eduorder.other.R;
import com.lanling.eduorder.other.ZhenhuaException;
import com.lanling.eduorder.service.OrderService;
import com.lanling.eduorder.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-03-13
 */
@RestController
@RequestMapping("/eduorder/order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    //1 生成订单的方法
    @PostMapping("createOrder/{courseId}")
    public R saveOrder(@PathVariable String courseId, HttpServletRequest request) {
        //创建订单，返回订单号
        String orderNo = orderService.createOrders(courseId, JwtUtils.getMemberIdByJwtToken(request.getHeader("usertoken")));
        return R.ok().data("orderId",orderNo);
    }

    //2 根据订单id查询订单信息
    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("item",order);
    }

    //根据课程id和用户id查询订单表中订单状态
    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable String courseId,@PathVariable String memberId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        wrapper.eq("status",1);//支付状态 1代表已经支付
        int count = orderService.count(wrapper);
        if(count>0) { //已经支付
            return true;
        } else {
            return false;
        }
    }

    //分组查询所有订单信息
    @PostMapping("getAllOrders/{current}/{limit}")
    public R getAllOrders(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) OrderQuery orderQuery){
        Page<Order> orderPage = new Page(current,limit);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        String courseTitle = orderQuery.getCourseTitle();
        String teacherName = orderQuery.getTeacherName();
        String nickname = orderQuery.getNickname();
        Integer payType = orderQuery.getPayType();
        Integer status = orderQuery.getStatus();
        //课程标题
        if (!StringUtils.isEmpty(courseTitle)){
            queryWrapper.like("course_title",courseTitle);
        }
        //老师姓名
        if (!StringUtils.isEmpty(teacherName)){
            queryWrapper.like("teacher_name",teacherName);
        }
        //学生姓名
        if (!StringUtils.isEmpty(nickname)){
            queryWrapper.like("nickname",nickname);
        }
        //支付类型
        if (!StringUtils.isEmpty(payType)){
            queryWrapper.eq("pay_type",payType);
        }
        //订单状态
        if (!StringUtils.isEmpty(status)){
            queryWrapper.eq("status",status);
        }
        //排序
        queryWrapper.orderByDesc("gmt_create");

        orderService.page(orderPage,queryWrapper);
        List<Order> orders = orderPage.getRecords();
        long now_current = orderPage.getCurrent();
        long pages = orderPage.getPages();
        long size = orderPage.getSize();
        long total = orderPage.getTotal();

        System.out.println("total1:---"+total);

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", orders);
        map.put("current", now_current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        return R.ok().data(map);
    }

    //根据订单编号逻辑删除订单
    @GetMapping("removeOrderById/{id}")
    public R removeOrderById(@PathVariable String id){
        Order order = new Order();
        order.setId(id);
        order.setIsDeleted(1);
        boolean b = orderService.updateById(order);
        if (!b){
            throw new ZhenhuaException(20001,"删除错误！");
        }else {
            return R.ok();
        }
    }

    //根据用户id来查询用户购买的订单
    @GetMapping("getOrdersByUserId")
    public R getOrdersByUserId(HttpServletRequest request){
        List<Order> orders = orderService.getOrdersByUserId(request);
        if (orders == null){
            return R.error().data("isSuccess",false);
        }else{
            return R.ok().data("orders",orders);
        }
    }

}

